package com.yiban.demo.LoginUser.Service.ServiceImp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiban.demo.LoginUser.DAO.InvitationCodeMapper;
import com.yiban.demo.LoginUser.DAO.LoginUserMapper;
import com.yiban.demo.LoginUser.Entity.InvitationCode;
import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import com.yiban.demo.LoginUser.Entity.LoginUserVO;
import com.yiban.demo.LoginUser.Entity.ROLE;
import com.yiban.demo.LoginUser.Service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.UUID;

//服务的具体实现的命名规范是xxServiceImp
//service层对参数进行处理，比如，核验，转化，同时使用多个mapper
@Service
public class LoginUserServiceImp implements LoginUserService {
    private static final  String REGISTER_MAIL_CODE_PREFIX = "register";

    //bean相同类属性互相拷贝，这个的实现基于字节码较为高效
    private final BeanCopier POToVoCopier = BeanCopier.create(LoginUserPO.class, LoginUserVO.class, false);
    private final BeanCopier VOToPoCopier = BeanCopier.create(LoginUserVO.class, LoginUserPO.class, false);
    private static final char[] CHARS= {1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};


    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private InvitationCodeMapper invitationCodeMapper;


    @Override
    public LoginUserPO register(LoginUserVO loginUserVO) {
        LoginUserPO userPO = loginUserMapper.selectOneByUsername(loginUserVO.getUsername());
        if (userPO != null) {
            return null;
        }
        if (loginUserVO.getNickname() == null || loginUserVO.getNickname().isBlank()) {
            loginUserVO.setNickname("njupt"+ UUID.randomUUID());
        }
        userPO = transToLoginPo(loginUserVO);
        loginUserMapper.insertOne(userPO);
        userPO.setRole(ROLE.USER.getName());
        return userPO;
    }

    @Override
    public LoginUserPO loginIn(LoginUserVO loginUserVO) {
        final LoginUserPO userPO = loginUserMapper.selectOneByUsername(loginUserVO.getUsername());
        if (userPO != null && userPO.getPassword().equals(loginUserVO.getPassword())){
            return userPO;
        }
        return null;
    }


    @Override
    public boolean modifyPassword(LoginUserPO nowUser, String oldPassword, String newPassword) {
        if (nowUser.getPassword().equals(oldPassword)) {
            return loginUserMapper.updatePasswordById(nowUser.getUserId(), newPassword);
        }
        return false;
    }



    @Override
    public LoginUserPO transToLoginPo(LoginUserVO loginUserVO) {
        final LoginUserPO userPO = new LoginUserPO();
        VOToPoCopier.copy(loginUserVO, userPO, null);
        return userPO;
    }
    @Override
    public LoginUserVO transToLoginVo(LoginUserPO loginUserPO) {
        final LoginUserVO vo = new LoginUserVO();
        POToVoCopier.copy(loginUserPO,vo,null);
        vo.setPassword("");
        return vo;
    }

    @Override
    public boolean userInvitationCode(String codeContent) {
        return invitationCodeMapper.useCode(codeContent) == 1;
    }

    @Override
    public InvitationCode getNewInvitationCode() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            builder.append(CHARS[((int) (Math.random() * CHARS.length))]);
        }
        InvitationCode invitationCode = new InvitationCode(null, builder.toString(), false);
        invitationCodeMapper.insert(invitationCode);
        return invitationCode;
    }
}
